<template>
  <div>
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学员信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>学生管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用户列表卡片区 -->
    <el-card class="box-card">
      <el-form :inline="true" ref="form" :model="studentName" label-width="100px" size="small">
        <el-form-item label="学生姓名">
          <el-input
              @keyup.enter.native="searchStudent"
              @clear="searchStudent"
              clearable
              v-model="queryMap.studentName"
              placeholder="请输入咨询者姓名查询"
          ></el-input>
        </el-form-item>

        <el-form-item label="联系方式">
          <el-input clearable @clear="searchStudent" v-model="queryMap.studentPhone" placeholder="请输入联系方式"></el-input>
        </el-form-item>

        <el-form-item label="班级">
          <el-select
              clearable
              @change="searchStudent"
              @clear="searchStudent"
              v-model="queryMap.classId"
              placeholder="请选择所属班级"
          >
            <el-option
                v-for="classInfo in classInfos"
                :label="classInfo.classname"
                :key="classInfo.classId"
                :value="classInfo.classId"
            >
              <span style="float: left">{{ classInfo.classname }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学员状态">
          <el-select
              clearable
              v-model="queryMap.studentStatus"
              @clear="searchStudent"
              placeholder="请选择状态"
          >
            <el-option label="未报名" value="0"></el-option>
            <el-option label="在读" value="1"></el-option>
            <el-option label="退学" value="2"></el-option>
            <el-option label="休学" value="3"></el-option>
            <el-option label="开除" value="4"></el-option>
            <el-option label="自主就业" value="5"></el-option>
            <el-option label="正常毕业" value="6"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left:50px;">
          <el-button  @click="reset" icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" @click="searchStudent" icon="el-icon-search">查询</el-button>
          <el-button
              type="success"
              icon="el-icon-plus"
              @click="addDialogVisible=true"
              v-hasPermission="'consult:add'"
          >添加</el-button>
          <el-button @click="downExcel" v-hasPermission="'consult:export'"  icon="el-icon-download">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table v-loading="loading" size="small" :data="studentList" border style="width: 100%;" height="420">
        <!-- <el-table-column type="selection" width="40"></el-table-column> -->
        <el-table-column label="#" prop="studentId" width="50"></el-table-column>
        <el-table-column prop="studentName" label="学生姓名" width="90"></el-table-column>


        <el-table-column prop="studentGender"  label="性别" width="60">
        <template slot-scope="scope">
          <span>{{showSex(scope.row)}}</span>
        </template>
        </el-table-column>
        <el-table-column prop="className" label="所属班级" width="100"></el-table-column>
        <el-table-column prop="brithday" label="出生日期" width="120" sortable></el-table-column>
        <el-table-column prop="studentPhone" label="联系方式" width="120" sortable></el-table-column>
        <el-table-column prop="qqAccount" label="QQ号码" width="120"></el-table-column>
        <el-table-column prop="wechatAaccout" label="微信账号" width="120"></el-table-column>
        <el-table-column prop="studentStatus" label="学生状态" width="110">
          <template slot-scope="scope">
            <span>{{showStatus(scope.row)}}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button   v-hasPermission="'student:edit'" size="small" type="primary" icon="el-icon-edit-outline" @click="edit(scope.row.studentId)"></el-button>
<!--            <el-button v-hasPermission="'student:delete'" type="danger" size="small" icon="el-icon-delete" @click="del(scope.row.studentId)"></el-button>-->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
          style="margin-top:10px;"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryMap.pageNo"
          :page-sizes="[6, 10, 20, 30]"
          :page-size="queryMap.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>

      <!-- 添加对话框 -->
      <el-dialog title="添加学员信息" @close="closeDialog" :visible.sync="addDialogVisible" width="60%;">
        <!-- 表单 -->
        <span>
          <el-form
              :model="addForm"
              :label-position="labelPosition"
              :rules="addFormRules"
              ref="addFormRef"
              label-width="80px"
          >
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="学生姓名" prop="studentName">
                    <el-input v-model="addForm.studentName"></el-input>
                  </el-form-item>
                </div>
              </el-col>
               <el-col :span="11">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="性别" prop="studentGender">
                    <el-radio-group v-model="addForm.studentGender">
                      <el-radio label="1" :value="1">男</el-radio>
                      <el-radio label="0" :value="0">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="联系电话" prop="studentPhone">
                    <el-input v-model="addForm.studentPhone"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="年龄" prop="studentAge">
                    <el-input v-model="addForm.studentAge"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="QQ号码" prop="qqAccount">
                    <el-input v-model="addForm.qqAccount"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="微信账号" prop="wechatAaccout">
                    <el-input v-model="addForm.wechatAaccout"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="身份证" prop="studentCard">
                    <el-input v-model="addForm.studentCard"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">

                 <el-form-item label="学生状态" prop="studentStatus">
                    <el-select v-model="addForm.studentStatus" style="width: 185px;" placeholder="请选择状态">
                      <el-option label="未报名" value="0"></el-option>
                      <el-option label="在读" value="1"></el-option>
                      <el-option label="退学" value="2"></el-option>
                      <el-option label="休学" value="3"></el-option>
                      <el-option label="开除" value="4"></el-option>
                      <el-option label="自主就业" value="5"></el-option>
                      <el-option label="正常毕业" value="6"></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item prop="brithday" label="出生日期">
                    <el-col :span="12">
                      <el-date-picker
                      type="date"
                      value-format="yyyy年MM月dd日"
                      placeholder="选择出生日期"
                      v-model="addForm.brithday"
                      style="width: 185px;"
                    ></el-date-picker>
                  </el-col>
                </el-form-item>
                </div>
              </el-col>
               <el-col :span="11">
                <div class="grid-content bg-purple-light">
                  <!--入学日期-->
                  <el-form-item prop="enrolTime" label="入学日期">
                    <el-col :span="12">
                      <el-date-picker
                          type="date"
                          value-format="yyyy年MM月dd日"
                          placeholder="选择入学日期"
                          v-model="addForm.enrolTime"
                          style="width: 185px;"
                      ></el-date-picker>
                    </el-col>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                    <!--毕业时间-->
                  <el-form-item prop="enrolTime" label="毕业日期">
                    <el-col :span="12">
                      <el-date-picker
                          type="date"
                          value-format="yyyy年MM月dd日"
                          placeholder="选择毕业日期"
                          v-model="addForm.graduateTime"
                          style="width: 185px;"
                      ></el-date-picker>
                    </el-col>
                  </el-form-item>
                </div>
              </el-col>
               <el-col :span="11">
                <div class="grid-content bg-purple">
                    <el-form-item label="学历提升" prop="educationPromotion">
                      <el-select v-model="addForm.educationPromotion" placeholder="请选择学历提升类型">
                        <el-option label="高升专" value="1"></el-option>
                        <el-option label="高起本" value="2"></el-option>
                        <el-option label="专升本" value="3"></el-option>
                        <el-option label="自考" value="4"></el-option>
                      </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
          <el-row>
            <el-col :span="11">
               <div class="grid-content bg-purple">
                 <el-form-item label="咨询师" prop="planner">
                  <el-input v-model="addForm.planner"></el-input>
                </el-form-item>
              </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="教育程度" prop="enrolEducation">
                    <el-input v-model="addForm.enrolEducation"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

             <el-form-item label="家庭住址" prop="studnetAddress">
              <el-input v-model="addForm.studnetAddress"></el-input>
            </el-form-item>

             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="年级" prop="gradeId">
                    <el-select v-model="addForm.gradeId" placeholder="请选择年级">
                      <el-option
                          v-for="grade in grades"
                          :key="grade.gradeId"
                          :label="grade.gradeName"
                          :value="grade.gradeId"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">

                 <el-form-item label="班级" prop="classId">
                    <el-select v-model="addForm.classId" placeholder="请选择班级">
                     <el-option
                         v-for="classInfo in classInfos"
                         :key="classInfo.classId"
                         :label="classInfo.classname"
                         :value="classInfo.classId"
                     ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            
            <el-form-item label="毕业学校" prop="graduateSchool">
              <el-input v-model="addForm.graduateSchool"></el-input>
            </el-form-item>
            
              <el-form-item label="邮箱" prop="email">
              <el-input v-model="addForm.email"></el-input>
            </el-form-item>

             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="家长姓名" prop="parentName">
                    <el-input v-model="addForm.parentName"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                 <el-form-item label="家长联系方式" prop="parentMoblie">
                    <el-input v-model="addForm.parentMoblie"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

              <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="学费出资人" prop="tuitionFeePayer">
                    <el-input v-model="addForm.tuitionFeePayer"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="家长是否支持" prop="isSupport">
                    <el-radio-group v-model="addForm.isSupport">
                      <el-radio :label="0" :value="0">是</el-radio>
                      <el-radio :label="1" :value="1">否</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                 <el-form-item label="青鸟通行证号" prop="qingNiaoPassNo">
                  <el-input v-model="addForm.qingNiaoPassNo"></el-input>
                </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                 <el-form-item label="青鸟通行证密码" prop="qingNiaoPassNoPwd">
                  <el-input v-model="addForm.qingNiaoPassNoPwd"></el-input>
                </el-form-item>
                </div>
              </el-col>
            </el-row>


                 <el-form-item label="是否住宿" prop="isStay">
                        <el-radio-group v-model="addForm.isStay">
                          <el-radio :label="1">住校</el-radio>
                          <el-radio :label="2">走读</el-radio>
                          <el-radio :label="3">其他</el-radio>
                        </el-radio-group>
                 </el-form-item>


          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button
              type="primary"
              @click="addStudent"
              :loading="btnLoading"
              :disabled="btnDisabled"
          >确 定</el-button>
        </span>
      </el-dialog>
      
      
      <!-- 修改对话框 -->
      <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editClose">
        <span>
          <el-form
              :model="editForm"
              :label-position="labelPosition"
              :rules="addFormRules"
              ref="editFormRef"
              label-width="80px"
          >
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="学生姓名" prop="studentName">
                    <el-input v-model="editForm.studentName"></el-input>
                  </el-form-item>
                </div>
              </el-col>
               <el-col :span="11">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="性别" prop="studentGender">
                    <el-radio-group v-model="editForm.studentGender">
                      <el-radio label="1" :value="1">男</el-radio>
                      <el-radio label="0" :value="0">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="联系电话" prop="studentPhone">
                    <el-input v-model="editForm.studentPhone"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="年龄" prop="studentAge">
                    <el-input v-model="editForm.studentAge"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="QQ号码" prop="qqAccount">
                    <el-input v-model="editForm.qqAccount"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="微信账号" prop="wechatAaccout">
                    <el-input v-model="editForm.wechatAaccout"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="身份证" prop="studentCard">
                    <el-input v-model="editForm.studentCard"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">

                 <el-form-item label="学生状态" prop="studentStatus">
                    <el-select v-model="editForm.studentStatus" style="width: 185px;" placeholder="请选择状态">
                      <el-option label="未报名" :value="0"></el-option>
                      <el-option label="在读" :value="1"></el-option>
                      <el-option label="退学" :value="2"></el-option>
                      <el-option label="休学" :value="3"></el-option>
                      <el-option label="开除" :value="4"></el-option>
                      <el-option label="自主就业" :value="5"></el-option>
                      <el-option label="正常毕业" :value="6"></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item prop="brithday" label="出生日期">
                    <el-col :span="12">
                      <el-date-picker
                          type="date"
                          value-format="yyyy年MM月dd日"
                          placeholder="选择出生日期"
                          v-model="editForm.brithday"
                          style="width: 185px;"
                      ></el-date-picker>
                  </el-col>
                </el-form-item>
                </div>
              </el-col>
               <el-col :span="11">
                <div class="grid-content bg-purple-light">
                  <!--入学日期-->
                  <el-form-item prop="enrolTime" label="入学日期">
                    <el-col :span="12">
                      <el-date-picker
                          type="date"
                          value-format="yyyy年MM月dd日"
                          placeholder="选择入学日期"
                          v-model="editForm.enrolTime"
                          style="width: 185px;"
                      ></el-date-picker>
                    </el-col>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                    <!--毕业时间-->
                  <el-form-item prop="enrolTime" label="毕业日期">
                    <el-col :span="12">
                      <el-date-picker
                          type="date"
                          value-format="yyyy年MM月dd日"
                          placeholder="选择毕业日期"
                          v-model="editForm.graduateTime"
                          style="width: 185px;"
                      ></el-date-picker>
                    </el-col>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                    <el-form-item label="学历提升" prop="educationPromotion">
                      <el-select v-model="editForm.educationPromotion" placeholder="请选择学历提升类型">
                        <el-option label="高升专" :value="1"></el-option>
                        <el-option label="高起本" :value="2"></el-option>
                        <el-option label="专升本" :value="3"></el-option>
                        <el-option label="自考" :value="4"></el-option>
                      </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
          <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="咨询师" prop="planner">
                    <el-input v-model="editForm.planner"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="教育程度" prop="enrolEducation">
                    <el-input v-model="editForm.enrolEducation"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

             <el-form-item label="家庭住址" prop="studnetAddress">
              <el-input v-model="editForm.studnetAddress"></el-input>
            </el-form-item>

             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="年级" prop="gradeId">
                    <el-select v-model="editForm.gradeId" placeholder="请选择年级">
                      <el-option
                          v-for="grade in grades"
                          :key="grade.gradeId"
                          :label="grade.gradeName"
                          :value="grade.gradeId"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">

                 <el-form-item label="班级" prop="classId">
                    <el-select v-model="editForm.classId" placeholder="请选择班级">
                     <el-option
                         v-for="classInfo in classInfos"
                         :key="classInfo.classId"
                         :label="classInfo.classname"
                         :value="classInfo.classId"
                     ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-form-item label="毕业学校" prop="graduateSchool">
              <el-input v-model="editForm.graduateSchool"></el-input>
            </el-form-item>

              <el-form-item label="邮箱" prop="email">
              <el-input v-model="editForm.email"></el-input>
            </el-form-item>

             <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="家长姓名" prop="parentName">
                    <el-input v-model="editForm.parentName"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                 <el-form-item label="家长联系方式" prop="parentMoblie">
                    <el-input v-model="editForm.parentMoblie"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

              <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                  <el-form-item label="学费出资人" prop="tuitionFeePayer">
                    <el-input v-model="editForm.tuitionFeePayer"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                  <el-form-item label="家长是否支持" prop="isSupport">
                    <el-radio-group v-model="editForm.isSupport">
                      <el-radio :label="0" :value="0">是</el-radio>
                      <el-radio :label="1" :value="1">否</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="11">
                <div class="grid-content bg-purple">
                 <el-form-item label="青鸟通行证号" prop="qingNiaoPassNo">
                  <el-input v-model="editForm.qingNiaoPassNo"></el-input>
                </el-form-item>
                </div>
              </el-col>
              <el-col :span="11">
               <div class="grid-content bg-purple">
                 <el-form-item label="青鸟通行证密码" prop="qingNiaoPassNoPwd">
                  <el-input v-model="editForm.qingNiaoPassNoPwd"></el-input>
                </el-form-item>
                </div>
              </el-col>
            </el-row>


                 <el-form-item label="是否住宿" prop="isStay">
                        <el-radio-group v-model="editForm.isStay">
                          <el-radio :label="1">住校</el-radio>
                          <el-radio :label="2">走读</el-radio>
                          <el-radio :label="3">其他</el-radio>
                        </el-radio-group>
                 </el-form-item>


          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button
              type="primary"
              @click="updateStudent"
              :loading="btnLoading"
              :disabled="btnDisabled"
          >确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    const checkEmail = (rule, value, callback) => {
      const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
      if (!value) {
        return callback(new Error("邮箱不能为空"));
      }
      setTimeout(() => {
        if (mailReg.test(value)) {
          callback();
        } else {
          callback(new Error("请输入正确的邮箱格式"));
        }
      }, 100);
    };
    const checkIdCard = (rule, value, callback) => {
      const mailReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (!value) {
        return callback(new Error("身份证不能为空"));
      }
      setTimeout(() => {
        if (mailReg.test(value)) {
          callback();
        } else {
          callback(new Error("请输入正确的身份证号码"));
        }
      }, 100);
    };
    const checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[34578]\d{9}$$/;
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(+value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (phoneReg.test(value)) {
            callback();
          } else {
            callback(new Error("电话号码格式不正确"));
          }
        }
      }, 100);
    };
    return {
      btnLoading: false,
      btnDisabled: false,
      departments: [],
      loading: true,
      total: 0,
      addDialogVisible: false, //添加对话框,
      editDialogVisible: false, //修改对话框
      assignDialogVisible: false, //分配角色对话框
      labelPosition: "right", //lable对齐方式
      //查询对象
      queryMap: {
        pageNum: 1,
        pageSize: 6,
        studentName: "",
        studentPhone: "",
        classId: "",
        studentStatus:""
      },
      studentList: [],

      addForm: {
        studentName: "",
        studentNumber: "",
        studentGender: "",
        studentAge: "",
        studentCard: "",
        brithday: "",
        studentPhone: "",
        enrolTime: "",
        enrolEducation: "",
        educationPromotion: "",
        studnetAddress: "",
        gradeId: "",
        qqAccount: "",
        wechatAaccout: "",
        graduateSchool: "",
        graduateTime: "",
        parentName: "",
        parentMoblie: "",
        tuitionFeePayer: "",
        isSupport: "",
        qingNiaoPassNo: "",
        qingNiaoPassNoPwd: "",
        specialInstructions: "",
        isStay: "",
        planner: "",
        studentStatus: "",
        email:"",
        classId:""
      }, //添加表单
      editForm: {}, //更新表单
      addFormRules: {
        studentName: [
          { required: true, message: "请输入咨询者名字", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        studentGender: [{ required: true, message: "请选择性别", trigger: "blur" }],
        studentPhone: [
          {
            required: true,
            message: "请输入联系方式",
            validator: checkPhone,
            trigger: "blur"
          }
        ],
        careerPlanner: [
          { required: true, message: "请输入咨询师", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        email: [{ required: true, validator: checkEmail, trigger: "blur" }],
        studentCard: [{ required: true, validator: checkIdCard, trigger: "blur" }],
      }
    };
  },
  methods: {

    /**
     * 重置
     */
    reset(){
      this.queryMap= {
        pageNum: 1,
        pageSize: 6,
        studentName: "",
        studentPhone: "",
        classId: "",
        studentStatus:""
      };
    },
    /**
     * 加载学员信息表格
     */
    downExcel() {
      const $this = this;
      const res = axios
          .request({
            url: "http://47.99.186.44:8990/student/manage/excel",
            method: "post",
            responseType: "blob"
          })
          .then(res => {
            if (res.headers["content-type"] === "application/json") {
              return $this.$message.error(
                  "Subject does not have permission [user:export]"
              );
            }
            const data = res.data;
            let url = window.URL.createObjectURL(data); // 将二进制文件转化为可访问的url
            const a = document.createElement("a");
            document.body.appendChild(a);
            a.href = url;
            a.download = "学生信息列表.xls";
            a.click();
            window.URL.revokeObjectURL(url);
          });
    },

    /**
     * 加载学员列表
     */
    async getStudentList() {
      const { data: res } = await this.$http.get("student/findList", {
        params: this.queryMap
      });
      if(!res.success){
        return this.$message.error("获取学员列表失败:"+res.data.errorMsg);
      }
      this.total = res.data.total;
      this.studentList = res.data.rows;
    },

    /**
     * 删除学员
     */
    async del(id) {
      const res = await this.$confirm(
          "此操作将永久删除该学员, 是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
      if (res === "confirm") {
        const { data: res } = await this.$http.delete("student/delete/" + id);
        console.log(res);
        if(res.success){
          this.$notify.success({
            title:'操作成功',
            message:'学员删除成功',
          });
          await this.getStudentList();
        }else {
          this.$message.error(res.data.errorMsg);
        }
      }
    },
    /**
     * 添加学员信息
     */
    async addStudent() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return;
        } else {
          this.btnLoading = true;
          this.btnDisabled = true;
          const { data: res } = await this.$http.post("student/add", this.addForm);
          if(res.success){
            this.$notify.success({
              title:'操作成功',
              message:'学员添加成功',
            });
            this.addForm = {};
            await this.getStudentList();
            await this.getGrades();
            await this.getClasss();
          } else {
            return this.$message.error("学员添加失败:" + res.data.errorMsg);
          }
          this.addDialogVisible = false;
          this.btnLoading = false;
          this.btnDisabled = false;
        }
      });
    },
    /**
     * 更新学员信息
     */
    async updateStudent() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          return;
        } else {
          this.btnLoading = true;
          this.btnDisabled = true;
          const { data: res } = await this.$http.put(
              "student/update/" + this.editForm.studentId,
              this.editForm
          );
          if(res.success){
            this.$notify({
              title: "操作成功",
              message: "学员基本信息已更新",
              type: "success"
            });
            this.editForm = {};
            await this.getStudentList();
            await this.getGrades();
            await this.getClasss();
          } else {
            this.$message.error("学员信息更新失败:" + res.data.errorMsg);
          }
          this.editDialogVisible = false;
          this.btnLoading = false;
          this.btnDisabled = false;
        }
      });
    },
    /**
     * 搜索学员
     */
    searchStudent() {
      this.queryMap.pageNum = 1;
      this.getStudentList();
    },
    /**
     * 修改学员信息
     */
    async edit(id) {
      const { data: res } = await this.$http.get("student/edit/" + id);
      if(res.success){
        this.editForm = res.data;
        this.editDialogVisible = true;
      } else {
        return this.$message.error("学员信息编辑失败:" + res.data.errorMsg);
      }
    },
    /**
     *  改变页码
     */
    handleSizeChange(newSize) {
      this.queryMap.pageSize = newSize;
      this.getStudentList();
    },
    /**
     * 翻页
     */
    handleCurrentChange(current) {
      this.queryMap.pageNum = current;
      this.getStudentList();
    },

    /**
     * 关闭添加弹出框
     */
    closeDialog() {
      this.$refs.addFormRef.clearValidate();
      this.addForm.birth = "";
      this.addForm = {};
    },
    /**
     * 关闭编辑弹出框
     */
    editClose() {
      this.$refs.editFormRef.clearValidate();
      this.editForm = {};
    },

    /**
     * 加载所有班级
     */
    async getClasss() {
      const { data: res } = await this.$http.get("class/findAll");
      if(!res.success){
        return this.$message.error("获取班级列表失败:"+res.data.errorMsg);
      }
      this.classInfos = res.data;
    },

    /**
     * 加载所有年级
     */
    async getGrades() {
      const { data: res } = await this.$http.get("grade/findAll");
      if(!res.success){
        return this.$message.error("获取班级列表失败:"+res.data.errorMsg);
      }
      this.grades = res.data;
    },


    /**
     * 显示用户性别
     */
    showSex(row, column) {
      let sex='';
      if(row.studentGender==1){
        sex = '男';
      }else {
        sex = '女';
      }
      return sex;
    },

    /**
     * 显示状态
     */
    showStatus(row, column) {
      let statusName = '';
      if(row.studentStatus===0){
        statusName ='未报名';
      }else if(row.studentStatus===1){
        statusName ='在读';
      }else if(row.studentStatus===2){
        statusName ='退学';
      }else if(row.studentStatus===3){
        statusName ='休学';
      }else if(row.studentStatus===4){
        statusName ='开除';
      }else if(row.studentStatus===5){
        statusName ='自主就业';
      }else if(row.studentStatus===6){
        statusName ='正常毕业';
      }
      return statusName;
    },
  },
  created() {
    this.getStudentList();
    this.getGrades();
    this.getClasss();
    setTimeout(() => {
      this.loading = false;
    }, 500);
  }
};
</script>
